package com.push.common.utils;

import com.push.common.config.SpiderConfig;
import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.config.DriverManagerType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;

public class ChromeHttpClientManager {
    @Autowired
    private SpiderConfig spiderConfig;

    private WebDriver driver;
    /**
     WebDriverManager.chromedriver().setup();
     WebDriverManager.firefoxdriver().setup();
     WebDriverManager.operadriver().setup();
     WebDriverManager.phantomjs().setup();
     WebDriverManager.edgedriver().setup();
     WebDriverManager.iedriver().setup();
     */
    public ChromeHttpClientManager(boolean inDemean, boolean useProxy) {
        //自动下载安装chromedriver
        DriverManagerType driverType = DriverManagerType.CHROME;
        if (useProxy){
            WebDriverManager.getInstance(driverType).proxy(spiderConfig.getProxyIp()+":"+spiderConfig.getProxyPort()).proxyUser(spiderConfig.getProxyUser()).proxyPass(spiderConfig.getProxyPass()).setup();
        } else {
            WebDriverManager.getInstance(driverType).setup();
        }
        if (inDemean) {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("headless");
            options.addArguments("start-fullscreen");
            options.addArguments("blink-settings=imagesEnabled=false");
            options.addArguments("disable-gpu");
            options.addArguments("no-sandbox");
            options.addArguments("disable-dev-shm-usage");
            driver = new ChromeDriver(options);
        } else {
            driver = new ChromeDriver();
        }
    }

    public ChromeHttpClientManager() {
        this(false, false);
    }

    public ChromeHttpClientManager(boolean inDemean) {
        this(inDemean, false);
    }

    public WebDriver getDriver(){
        return driver;
    }
}
